# -*- coding:utf-8 -*-
'''
@author: xiaoming
@contact: lishihui0129@163.com
@time: 2017/6/29 16:02
@desc:
'''
import re
import os
from myUtil import weichatPattern,flagSplitPattern
from mysqlHelper import close,connectDB,insertSql


def weichatExtract(string):
    '''
    :param string: 待提取对象
    :return:提取结果
    '''
    # print "======================================"
    # print string
    # print "======================================"

    # res=re.findall(weichatPattern,string)
    # res=list(set(res))
    # flag=prefixFlag(string)
    # string=string.replace('微信','')

    weizhi=string.find(u'微信')
    if weizhi==-1:
        weizhi=0

    string1=(string[weizhi:]).replace(u'微信',u'')
    temp=[]
    # for item in res:
    #     linshi=item
    # print "-------------------------------"
    # print string1
    # print "-------------------------------"
    temp.append(string1.strip())
    return temp


def prefixFlag(string):
    '''
    :param string: 待提取对象
    :return:前缀信息
    '''
    if '微信' in string:
        ind=string.index('微信')
    elif '公众号' in string:
        ind=string.index('公众号')
    elif 'v' in string:
        ind=string.index('V')
    else:
        ind=0
    temp=string[:ind]
    # res=re.split(r'，|:|!|,|;| |（|\(|\[|【',temp)
    res=re.split(flagSplitPattern,temp)
    return res[-1].strip()

def insertWeichatTable(string):
    res=re.findall(weichatPattern,string)
    res=list(set(res))
    flag=prefixFlag(string)
    connectDB()
    sql="insert into weichat(weichat_value,prefix) values(%s,%s)"

    for each in res:
        param=(each,flag)
        insertSql(sql,param)
    close()


if __name__=='__main__':
    workdir='.\output'
    fileList=os.listdir(workdir)
    if 'weichat.txt' in fileList:
        targetFile=os.path.join(workdir,'weichat.txt')
        res=open(targetFile).readlines()
        for line in res:
            if len(line.strip())!=0:
                print weichatExtract(line)
                insertWeichatTable(line)
            else:
                continue
    else:
        pass